import store from '@/store'

function checkPermission(el, binding) {
    const { value } = binding
    const role = store.getters.roles
    if (value && value instanceof Array) {
        if (value.length > 0) {
            const hasPermission = role.some(r => {
                return value.includes(r)
            })
            if (!hasPermission) {
                el.parentNode && el.parentNode.removeChild(el)
            }
        }
    } else {
        throw new Error('权限必须为数组!')
    }
}


export default {
    inserted(el, binding) {
        checkPermission(el, binding)
    },
    update(el, binding) {
        checkPermission(el, binding)
    }
}